package com.wenwu.controller.common;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wenwu.common.bean.RelicsDictionary;
import com.wenwu.common.bean.RelicsDictionaryExample;
import com.wenwu.common.page.PageUtils;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.common.utils.JsonUtils;
import com.wenwu.service.DictionaryService;

/**
 * @project RelicsProject-web
 * @class DictionaryController.java
 * @author zhujiagui E-mail:<zhujiagui@zkingsoft.com>
 * @date 2015年5月1日 下午4:35:25
 * @description 字典表管理模块
 */
@Controller(value = "DictionaryController")
@RequestMapping(value = "/mgr/dictionary")
public class DictionaryController {
	@Autowired
	private DictionaryService dictionaryService;
	Logger log = Logger.getLogger(this.getClass());
	// 用来创建json数据
	private JsonGenerator jsonGenerator = null;

	// 进入页面
	@RequestMapping(value = "/init")
	public String initUserPage() {
		log.info("进入字典管理页面...");
		return "dictionary/dictionarylist";
	}

	// 进入页面
	@RequestMapping(value = "/dictionaryform")
	public String dictionaryform() {
		log.info("进入字典表单页面...");
		return "dictionary/dictionaryform";
	}

	// 查询用户数据
	@RequestMapping(value = "/showList")
	public @ResponseBody PaginationVO showList(Model model,
			HttpServletRequest request, RelicsDictionary dictionary)
			throws Exception {
		log.info("查询数据...");
		PaginationVO pageVo = PageUtils.getPaginationInfo(request);
		try {
			int totalRecord = dictionaryService.selectTotalRecord(dictionary);// 查询符合条件的个数
			List<RelicsDictionary> list = dictionaryService.selectList(
					dictionary, pageVo);// 查询符合条件的数据
			pageVo.setList(list);// 将数据设置到PaginationVO对象中
			pageVo.setTotalRecord(totalRecord);
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage());
			model.addAttribute("errorMsg", e.getMessage());
		}
		return pageVo;
	}

	// 添加数据
	@RequestMapping(value = "/addObj")
	public void addObj(Model model, RelicsDictionary dictionary,
			HttpServletResponse response) throws Exception {
		jsonGenerator = JsonUtils.getJsonGenerator(response);
		log.info("添加数据...");
		try {
			jsonGenerator.writeObject(dictionaryService.addObj(dictionary));
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage());
			model.addAttribute("errorMsg", e.getMessage());
		}
	}

	/**
	 * 显示要修改的信息
	 * 
	 * @param response
	 * @param userId
	 * @throws Exception
	 */
	@RequestMapping(value = "/showUpdate")
	public void showUpdate(HttpServletResponse response,
			@RequestParam(value = "dictionaryId") Integer dictionaryId)
			throws Exception {
		if (dictionaryId != null) {
			RelicsDictionary dictionary = dictionaryService
					.selectObjByKey(dictionaryId);
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			jsonGenerator.writeObject(dictionary);
		}
	}

	/**
	 * 删除信息
	 * 
	 * @param response
	 * @param request
	 * @param key
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteInfo")
	public void deleteInfo(HttpServletResponse response,
			HttpServletRequest request,
			@RequestParam(required = false, value = "key") String key)
			throws Exception {
		log.info("主键：" + key);
		if (key != null) {
			List<String> keys = new ArrayList<String>();
			String[] objs = key.split(",");
			for (String k : objs) {
				keys.add(k);
			}
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			try {
				int result = dictionaryService.deleteInfoByKey(keys);
				jsonGenerator.writeObject("成功删除" + result + "条数据");
			} catch (Exception e) {
				jsonGenerator.writeObject("删除数据失败");
			}
		}
	}
	@RequestMapping(value = "/showListByKey")
	public @ResponseBody List<RelicsDictionary> showListByKey(Model model,@RequestParam(required = true, value = "dicKey")Integer dicKey)
			throws Exception {
		log.info("根据key值获取数据...");
		List<RelicsDictionary> list =null;
		try {
			RelicsDictionaryExample example=new RelicsDictionaryExample();
			example.createCriteria().andDicKeyEqualTo(dicKey);
			list= dictionaryService.selectListByExample(example);// 查询符合条件的数据
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage());
		}
		return list;
	}
}
